<template>
  <!-- 遮罩层 -->
  <Transition name="fade">
    <div v-if="loginStore.isLoginShow" class="modal-overlay">
      <div class="login-box">
        <div class="close-btn" @click="loginStore.isLoginShow = false">×</div>
        <div class="login-header">
          <h2>欢迎登录</h2>
          <p>请输入您的账号和密码</p>
        </div>
        
        <div class="login-form">
          <div class="form-item">
            <input style="width: 343px;"
              type="text" 
              v-model="loginStore.username" 
              placeholder="用户名"
            >
            <i class="icon-user"></i>
          </div>
          
          <div class="form-item">
            <input style="width: 343px;"
              type="password" 
              v-model="loginStore.password" 
              placeholder="密码"
            >
            <i class="icon-lock"></i>
          </div>

          <div class="remember-forgot">
            <label>
              <input type="checkbox" v-model="loginStore.remember">
              <span>记住我</span>
            </label>
            <!-- <a href="#">忘记密码?</a> -->
          </div>

          <button class="login-btn" @click="handleLogin">登录</button>
        </div>
      </div>
    </div>
  </Transition>
</template>

<script setup>
import { reactive, ref ,watch} from 'vue'
import { useLoginStore } from '@/store/login'
import { login} from '@/api/user'


const loginStore = useLoginStore()

function handleLogin(){
    let params = new URLSearchParams()
    params.append('username',loginStore.username)
    params.append('password',loginStore.password)
    params.append('remember',loginStore.remember)
    login(params).then(res=>{
        console.log(res)
        if(res.code === 1){
            localStorage.setItem('username',loginStore.username)
            localStorage.setItem('token',res.token)
            localStorage.setItem('user',JSON.stringify(res.user))
            loginStore.isLogin = true
            loginStore.isLoginShow = false
            loginStore.username = ''
            loginStore.password = ''
            location.reload()
        }
    })
}

</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.login-box {
  position: relative;
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  box-shadow: 0 15px 25px rgba(0, 0, 0, 0.1);
  animation: slideIn 0.3s ease-out;
}

.close-btn {
  position: absolute;
  top: 15px;
  right: 20px;
  font-size: 24px;
  color: #666;
  cursor: pointer;
  transition: color 0.3s;
}

.close-btn:hover {
  color: #333;
}

/* 动画相关样式 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

@keyframes slideIn {
  from {
    transform: translateY(-20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #333;
  font-size: 24px;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.form-item {
  position: relative;
  margin-bottom: 20px;
}

.form-item input {
  width: 100%;
  padding: 12px 40px 12px 15px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-item input:focus {
  border-color: #667eea;
  outline: none;
}

.form-item i {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  font-size: 14px;
}

.remember-forgot label {
  display: flex;
  align-items: center;
  color: #666;
}

.remember-forgot input[type="checkbox"] {
  margin-right: 5px;
}

.remember-forgot a {
  color: #667eea;
  text-decoration: none;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 5px;
  color: white;
  font-size: 16px;
  cursor: pointer;
  transition: transform 0.3s;
}

.login-btn:hover {
  transform: translateY(-2px);
}
</style>
